package person.drf.shop.product.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;
import person.drf.common.utils.PageUtils;
import person.drf.common.utils.Query;

import person.drf.shop.product.dao.BrandDao;
import person.drf.shop.product.dao.CategoryBrandRelationDao;
import person.drf.shop.product.entity.BrandEntity;
import person.drf.shop.product.service.BrandService;
import person.drf.shop.product.service.CategoryBrandRelationService;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            wrapper.eq("brand_id",key).or().like("name",key).or().like("descript",key);;
        }
        IPage<BrandEntity> page = this.page(new Query<BrandEntity>().getPage(params),wrapper );
        return new PageUtils(page);
    }

    @Override
    public void updateDetails(BrandEntity brand) {
        //先更新自己
        //保证数据一致性，要是本表数据改变，其他关联表也要一起改
        this.updateById(brand);

        //如果名字改了
        if(!StringUtils.isEmpty(brand.getName())){
            categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());

            //TODO;  更新其他关联
        }
    }

}